//
//  EditGymViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/5.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "EditGymViewController.h"
#import "SearchGymAddressViewController.h"
#import "ZLPhotoActionSheet.h"
#import "I_Media.h"
#import "EditInfoController.h"
#import "I_Gym.h"

@interface EditGymViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *gymIcon;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;

@property (weak, nonatomic) IBOutlet UILabel *addressLabel;




@property (nonatomic, strong) M_Gym *gym;
@end

@implementation EditGymViewController

#pragma mark Class Method
+ (instancetype)controllerWithGym:(M_Gym *)gym
{
    EditGymViewController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([EditGymViewController class]) inStoryBoard:@"GymManagement"];
    vc.gym = gym;
    return vc;
}


#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    if (_gym) {
        self.title = @"编辑门店";
    } else {
        self.title = @"新增门店";
        _gym = [[M_Gym alloc] init];
    }
    [self setRightBarButtonWithTitle:@"保存" target:self action:@selector(saveGym)];
    [self refreshUI];
    
}

- (void)refreshUI
{
    [_gymIcon setNetImageWithUrl:_gym.gymLogo];
    _nameLabel.text = _gym.gymName;
    _mobileLabel.text = _gym.gymContactinfo;
    _addressLabel.text = _gym.gymAddress;
    if (_gym.gymLogo.length == 0 || _gym.gymName.length == 0 || _gym.gymContactinfo.length == 0 || _gym.gymAddress.length == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
    [self.tableView reloadData];
}
- (void)saveGym{
    if (_gym.gymLogo.length == 0) {
        [AV showRemind:@"请选择门店Logo"];
        return;
    }
    
    if (_gym.gymName.length == 0) {
        [AV showErrorMessage:@"请选择门店名称"];
        return;
    }
    if (_gym.gymContactinfo.length == 0) {
        [AV showErrorMessage:@"请输入门店电话"];
        return;
    }
    if (_gym.gymAddress.length == 0) {
        [AV showErrorMessage:@"请选择门店地址"];
        return;
    }
    
    [AV showLoading:@"请稍后"];
    WeakSelf(self);
    [I_Gym editGymByAdmin:_gym success:^{
        if ([RI.currentGym.gymId isEqualToString:weakself.gym.gymId]) {//修改的是当前选中的门店
            RI.currentGym = weakself.gym;
        }
        [AV hideen];
        [weakself.navigationController popViewControllerAnimated:YES];
        POST_NOTIFY(kNotificationEditGymSuccess, nil);
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

#pragma mark UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {//门店Logo
        ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
        actionSheet.configuration.allowSelectGif = NO;
        actionSheet.configuration.allowSelectVideo = NO;
        actionSheet.configuration.allowSelectLivePhoto = NO;
        actionSheet.configuration.allowEditImage = YES;
        actionSheet.configuration.maxSelectCount = 1;
        actionSheet.configuration.navBarColor= HEXColor(@"#3AA7FF");
        actionSheet.configuration.navTitleColor = [UIColor whiteColor];
        actionSheet.configuration.bottomBtnsNormalTitleColor = HEXColor(@"3AA7FF");
        actionSheet.configuration.bottomBtnsDisableBgColor = RGBA(58, 167, 255, 0.5);
        actionSheet.configuration.editAfterSelectThumbnailImage = YES;
        actionSheet.configuration.clipRatios = @[GetClipRatio(1, 1)];
        actionSheet.sender = self;
        
        __weak typeof(self) weakSelf = self;
        [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nullable images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
            [AV showLoading:@"正在上传"];
            [I_Media upLoadImages:images success:^(NSArray *urls) {
                [AV hideen];
                weakSelf.gym.gymLogo = urls.firstObject;
                [weakSelf refreshUI];
            } failure:^(NSError *error) {
                [AV showErrorMessage:@"照片上传失败"];
            }];
        }];
        [actionSheet showPhotoLibrary];
    } else {
        if (indexPath.row == 0) {//门店名称
            SearchGymAddressViewController *searchGYMVC = [SearchGymAddressViewController controllerWithSearchType:SearchGYMAddressType_SearchName];
            WeakSelf(self);
            searchGYMVC.choiceBlock = ^(M_Gym *searchGym) {
                weakself.gym.gymAddress = searchGym.gymAddress;
                weakself.gym.gymAddressId = searchGym.gymAddressId;
                weakself.gym.gymCity = searchGym.gymCity;
                weakself.gym.gymDistrict = searchGym.gymDistrict;
                weakself.gym.gymLocationLatitude = searchGym.gymLocationLatitude;
                weakself.gym.gymLocationLongitude = searchGym.gymLocationLongitude;
                weakself.gym.gymName = searchGym.gymName;
                weakself.gym.gymProvince = searchGym.gymProvince;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:searchGYMVC animated:YES];
        } else if (indexPath.row == 2) {//门店地址
            SearchGymAddressViewController *searchGYMVC = [SearchGymAddressViewController controllerWithSearchType:SearchGYMAddressType_SearchAddress];
            WeakSelf(self);
            searchGYMVC.choiceBlock = ^(M_Gym *searchGym) {
                weakself.gym.gymAddress = searchGym.gymAddress;
                weakself.gym.gymAddressId = searchGym.gymAddressId;
                weakself.gym.gymCity = searchGym.gymCity;
                weakself.gym.gymDistrict = searchGym.gymDistrict;
                weakself.gym.gymLocationLatitude = searchGym.gymLocationLatitude;
                weakself.gym.gymLocationLongitude = searchGym.gymLocationLongitude;
//                weakself.gym.gymName = searchGym.gymName;
                weakself.gym.gymProvince = searchGym.gymProvince;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:searchGYMVC animated:YES];
        } else if (indexPath.row == 1) {//门店电话
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_GymMobile originStr:_gym.gymContactinfo];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.gym.gymContactinfo = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 95;
    }
    if (indexPath.row == 2) {
        CGFloat height = [_gym.gymAddress boundingRectWithSize:CGSizeMake((kDeviceWidth - 140 - 33), 300) font:_addressLabel.font].height;
        height += 28;
        if (height < 50) {
            height = 50;
        }
        return height;
    }
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


@end
